package com.xzj.admin.analysis.base.domain;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.constants.CommonEnums;
import com.xzj.admin.analysis.base.util.AnalysisCommonUtil;
import lombok.Data;

import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.function.BiConsumer;

/**
 * @program: xzj-admin
 * @ClassName: CommonFlowInfo
 * @description: 公用流转对像
 * @author: XiaZunJun
 * @create: 2024-11-18 17:56
 */
@Data
public class CommonFlowInfo {
    /**
     * 消息推送到kafka的时间
     */
    private Date messageDate;

    /**
     * 消息推送到kafka的时间字符串，格式：yyyyMMdd
     */
    private String dateStr;
    /**
     * 所属云资源
     */
    private String cloudId;
    /**
     * 消息所属省份 - 根据topic转化来
     */
    private String standardCode;

    /**
     * 消息所属虚拟资源池id， 根据dateHour转化
     */
    private String rmPoolId;

    /**
     * 消息源主题
     */
    private String sourceTheme;

    private String cloudType;

    /**
     * 线程计数器
     */
    private CountDownLatch downLatch;

    /**
     * 判断入中间库数据的标签函数方法
     */
    private BiConsumer<CommonFlowInfo, ResMessageManage> judgeRowTagFun;

    private String threadName;

    private String cacheKeyPrefix;

    private String subObjectKey;


    public void putCacheKeyPrefix(String sourceTheme){
        this.sourceTheme = sourceTheme;
        this.cacheKeyPrefix = AnalysisCommonUtil.unifyBuildRedisKey(this.cloudId,this.cloudType,this.rmPoolId,this.standardCode,null);
        this.subObjectKey = AnalysisCommonUtil.commonBuildStr(this.rmPoolId,this.standardCode,sourceTheme,this.threadName);
    }

    /**
     * 创建通用流转对象方法
     *
     * @param msgJson kafka数据
     * @param time    kafka接收数据时间
     * @return AnalysisConvertInfo
     * @author xiazunjun
     * @date 2023/9/26 9:19
     */
    public static CommonFlowInfo create(JSONObject msgJson, long time, String cloudId, String cloudType) {
        CommonFlowInfo info = new CommonFlowInfo();
        // 根据附加信息获取其虚拟云资源和身份信息
        JSONObject extendJson = msgJson.getJSONObject(CommonEnums.JSON_FIELD.EXTEND.getValue());
        info.setStandardCode(extendJson.getString("provinceId"));
        info.setRmPoolId(extendJson.getString("regionId"));
        info.setMessageDate(new Date(time));
        info.setCloudId(cloudId);
        String dateStr = DateUtil.format(info.getMessageDate(), "yyyyMMdd");
        info.setDateStr(dateStr);
        info.setCloudType(cloudType);
        info.setThreadName(Thread.currentThread().getName());
        return info;
    }
}
